import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { Router, NavigationStart, NavigationEnd, ActivatedRoute} from "@angular/router"; 
import { JsonApiService } from "app/core/api/json-api.service";
import { Title } from '@angular/platform-browser'

@Component({
	selector: 'app-root',
	template: '<router-outlet></router-outlet>'
})
export class AppComponent implements OnInit {
	resourceList:any[] = [];
	isLogin = false;
	public title = 'app works!';

	public constructor(private viewContainerRef: ViewContainerRef, private router: Router, private jsonApiService: JsonApiService, private activatedRoute: ActivatedRoute, private titleService: Title) {
		this.router.events.filter((event) => event instanceof NavigationStart)
		  .subscribe((event:NavigationStart) => {
		  	let currentUrl = window.location.hash;
		  	if(!currentUrl)return;
		  	if(currentUrl.includes('userName')&&currentUrl.includes('password')){
		  		let reqdata = 'userName='+this.getQueryString('userName')+'&password='+this.getQueryString('password');
		  		this.jsonApiService.http("login3.html",reqdata).subscribe(data=> {
		  			if(data.success){
						this.isLogin = true;
						common.setCookie('isLogin',"true",8);//登录前端设置8小时失效
						this.initMenu();
					}else{
						this.jsonApiService.message('您没有权限访问');
						common.clearCookie('isLogin');
					}
		  		})
		  	}
		})
	}
	ngOnInit() {
		this.router.events
		.filter(event => event instanceof NavigationEnd) // 获取 NavigationEnd 事件
		.map(() => this.activatedRoute) // 获取当前展示页面的路由信息
		.map(route => { // 处理子路由
			// 遍历路由表获取每一个页面的路由信息
			while(route.firstChild) {
				route = route.firstChild
			}
			return route
		})
		.mergeMap(route => route.data) // 获取每个路由配置的data信息
		.subscribe(event => { // 最后设置标题
			// event { pageTitle: '角色管理' }
			this.titleService.setTitle(event.pageTitle ? event.pageTitle + '-让行业物流更智慧' : '让行业物流更智慧')
		})
	}
	getQueryString(name) {
	    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	    let temSearchParam = window.location.hash.split('?')[1];
	    var r = temSearchParam.match(reg);
	    if (r != null) {
	        return r[2];
	    }
	    return null;
	}
	initMenu(){
		this.jsonApiService.http('index2.html',null).subscribe(
			(data) => {
				localStorage.clear();
				this.resourceList = [];
				if(data.success){
					var tempArr = data.data.menu;
					if(!tempArr || tempArr.length == 0){
						this.jsonApiService.message('您没有权限访问!具体咨询管理员');
						return;
					}
					for(let item of tempArr){
						if(item.id && !item.parentId){
							let obj = {
								children:[],
								btnArr:[],
								name:item.name,
								path:item.path,
								level:1,
								id:item.id,
								parentId:"",
								sourceKey:item.sourceKey,
								sourceUrl:item.sourceUrl,
								icon:item.icon,
								type:item.type
							}
							this.resourceList.push(obj)
						}
					}
					for (let i = 0; i < tempArr.length; i++){
						for(let j = 0; j <  this.resourceList.length; j++){
							if(tempArr[i].parentId && tempArr[i].parentId === this.resourceList[j].id){
								let obj = {
									children:[],
									btnArr:[],
									name:tempArr[i].name,
									level:2,
									path:tempArr[i].path,
									id:tempArr[i].id,
									parentId:tempArr[i].parentId,
									sourceKey:tempArr[i].sourceKey,
									sourceUrl:tempArr[i].sourceUrl,
									icon:tempArr[i].icon,
									type:tempArr[i].type
								}
								if(tempArr[i].type === 0){
									this.resourceList[j].children.push(obj);
								}else if(tempArr[i].type === 1){
									this.resourceList[j].btnArr.push(obj);
								}
							}
						}
					}
					for(let i = 0; i < this.resourceList.length; i++){
						for(let j = 0; j < this.resourceList[i].children.length;j++){
							for(let k = 0; k < tempArr.length; k++){
								if(this.resourceList[i].children[j]&&this.resourceList[i].children[j].id === tempArr[k].parentId){
									let obj = {
										children:[],
										btnArr:[],
										name:tempArr[k].name,
										level:3,
										id:tempArr[k].id,
										path:tempArr[k].path,
										parentId:tempArr[k].parentId,
										sourceKey:tempArr[k].sourceKey,
										sourceUrl:tempArr[k].sourceUrl,
										icon:tempArr[k].icon,
										type:tempArr[k].type
									}
									if(tempArr[k].type === 0){
										this.resourceList[i].children[j].children.push(obj);
									}else if(tempArr[k].type === 1){
										this.resourceList[i].children[j].btnArr.push(obj);
									}
								}
							}
						}
					}
					if(this.resourceList[0].children.length == 0){
						if(this.resourceList[0].path){
							localStorage.removeItem('s-menu');
							localStorage.setItem('s-menu',JSON.stringify(this.resourceList));//菜单缓存到本地
							localStorage.setItem('cargpsw_'+this.resourceList[0].sourceKey,JSON.stringify(this.resourceList[0].btnArr));
							this.router.navigate([this.resourceList[0].path]);//默认跳转第一条数据菜单,没有子菜单情况下,跳转父级
						}else{
							this.jsonApiService.message("您没有权限访问!具体咨询管理员")
						}
					}else{
						if(this.resourceList[0].children[0].path){
							localStorage.removeItem('s-menu');
							localStorage.setItem('s-menu',JSON.stringify(this.resourceList));//菜单缓存到本地
							localStorage.setItem('cargpsw_'+this.resourceList[0].children[0].sourceKey,JSON.stringify(this.resourceList[0].children[0].btnArr));
							this.router.navigate([this.resourceList[0].children[0].path]);//默认跳转子级第一个路由
						}else{
							this.jsonApiService.message("您没有权限访问!具体咨询管理员")
						}
					}
					
				}
			}
		)
	}
}